<?php

namespace App\Listeners\UserRecommend;

use App\Common\Libs\Dispatch\Console;
use App\Console\Commands\Controller\UserRecommendCommand;
use App\Consts\GlobalConsoleConst;
use App\Consts\GlobalConst;
use App\Consts\GlobalDynamicConst;
use App\Events\UserRecommend\CancelUserRecommendEvent;
use App\Listeners\BaseListener;

class CancelUserRecommendListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param CancelUserRecommendEvent $event
     * @return bool
     */
    public function handle(CancelUserRecommendEvent $event)
    {
        $this->saveFileLog(__CLASS__, $event);

        $userRecommend = $event->userRecommend;

        // 修改推荐卡片用户头像区域
        Console::getInstance(UserRecommendCommand::class)
            ->push([
                'target_id' => $userRecommend['target_id']
            ], GlobalConsoleConst::USER_RECOMMEND_AVATAR_AREA);

        // 队列记录日志
        service()->LogCountService->createLogByQueue($event->logCountLoadData);

        // 删除动态
        if ($userRecommend['target_type'] == GlobalConst::TARGET_TYPE_ARTICLE) {
            service()->DynamicService->delDynamicByArticle([
                'user_id' => $userRecommend['user_id'],
                'target_id' => $userRecommend['target_id'],
                'target_type' => $userRecommend['target_type'],
                'dynamic_type' => GlobalDynamicConst::DYNAMIC_TYPE_USER_RECOMMEND_ARTICLE
            ]);
        }

        return true;
    }
}
